/*
   Copyright (c) [2023] [魔凤啸天]
   [MFUI] is licensed under Mulan PSL v2.
   You can use this software according to the terms and conditions of the Mulan PSL v2. 
   You may obtain a copy of Mulan PSL v2 at:
            http://license.coscl.org.cn/MulanPSL2 
   THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND, EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT, MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE.  
   See the Mulan PSL v2 for more details.  
*/
#include "radiobutton.hpp"

namespace MFUI{
	void RadioButton::_changed(){
		if(getvalue()){
			auto cur=last;
			while(cur){
				cur->setvalue(false);
				cur=cur->last;
			}
			cur=next;
			while(cur){
				cur->setvalue(false);
				cur=cur->next;
			}
		}
		changed();
	}
	RadioButton::RadioButton(SDL_Color back,bool v):CheckButton(back,v),last(nullptr),next(nullptr){}
	RadioButton::~RadioButton(){
		if(last)last->deref();
		if(next)next->deref();
	}
	void RadioButton::disconnect(){
		if(last)last->next=next,deref();
		if(next)next->last=last,deref();
		last=nullptr,next=nullptr;
	}
	void RadioButton::connect(RadioButton* b){
		next=b;
		b->last=this;
		ref();
		b->ref();
	}
	void RadioButton::connect(std::initializer_list<RadioButton*> b){
		auto cur=this;
		for(auto v:b){
			cur->connect(v);
			cur=v;
		}
	}
}